package org.adream.account.rest.api;

import java.util.List;

import org.adream.account.entity.NationEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.service.NationService;
import org.adream.account.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/nation")
@Controller
public class NationApiController {
	
	@Autowired
	private NationService nationService;
	
	/**
	 * 新增民族信息
	 * @param nation
	 * @return
	 */
	@RequestMapping(value = "", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<String> addNation(@RequestBody NationEntity nation) {
		if(nation == null || StringUtils.isEmpty(nation.getNname())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "新增失败,请求参数有误", null);
		}
		return nationService.addNation(nation);
	}
	
	/**
	 * 删除民族信息
	 * @param nid
	 * @return
	 */
	@RequestMapping(value = "/{nid}", produces = "application/json;charset=UTF-8", method = RequestMethod.DELETE)
	@ResponseBody
	public ResultModel<String> deleteNation(@PathVariable("nid") Integer nid) {
		if(StringUtils.isEmpty(nid)) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "删除失败,请求参数有误", null);
		}
		return nationService.deleteNation(nid);
	}
	
	/**
	 * 更新民族信息
	 * @param nid
	 * @param nationEntity
	 * @return
	 */
	@RequestMapping(value = "/{nid}", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
	@ResponseBody
	public ResultModel<String> updateNation(@PathVariable("nid") Integer nid,@RequestBody NationEntity nationEntity) {
		if(StringUtils.isEmpty(nid) || nationEntity == null) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "更新失败,请求参数有误", null);
		}
		return nationService.updateNation(nid, nationEntity);
	}
	
	/**
	 * 根据nid,删除标志查询
	 * @param nid
	 * @param dr
	 * @return
	 */
	@RequestMapping(value = "/nid/{nid}/dr/{dr}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<NationEntity> queryNationByNidAndDr(@PathVariable("nid") Integer nid,@PathVariable("dr") Integer dr) {
		if(StringUtils.isEmpty(nid) || StringUtils.isEmpty(dr)) {
			return new ResultModel<NationEntity>(ResultModel.ERR_PARAM, "查询失败,请求参数有误", null);
		}
		NationEntity nation = nationService.queryNationByNidAndDr(nid, dr);
		if(nation == null) {
			return new ResultModel<NationEntity>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<NationEntity>(ResultModel.SUCCESS, "查询成功", nation);
	}
	
	/**
	 * 根据删除标志查询
	 * @param dr 删除标志
	 * @return
	 */
	@RequestMapping(value = "/dr/{dr}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public ResultModel<List<NationEntity>> queryNationByDr(@PathVariable("dr") Integer dr) {
		if(StringUtils.isEmpty(dr)) {
			return new ResultModel<List<NationEntity>>(ResultModel.ERR_PARAM, "查询失败,请求参数有误", null);
		}
		List<NationEntity> nations = nationService.queryNationByDr(dr);
		if(Utils.isEmptyCollection(nations)) {
			return new ResultModel<List<NationEntity>>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<List<NationEntity>>(ResultModel.SUCCESS, "查询成功", nations);
	}
}